<?php
$jumlah_debit=0;
$jumlah_credit = 0;
function printChildren($children,$htmlHelper,$number,&$jumlah_debit, &$jumlah_credit, $selected_month, $selected_year)
{?>

<?php foreach ($children as $account):?>
	<tr>
		<td><?php echo $account['nomor'] ?></td>
		<td><?php echo !empty($account['id'])? $htmlHelper->link($account['name'], '/accounting_reportings/buku_besar/'.$account['id'].'/'.$selected_month.'/'.date('Y')) : $account['name'] ?></td>
		<td class="money"><?php echo $account['saldo'] > 0 ? $number->currency($account['saldo']): $number->currency(0)?></td>
		<td class="money"><?php echo $account['saldo'] < 0 ? $number->currency($account['saldo']): $number->currency(0) ?></td>
		<?php $jumlah_debit += $account['saldo'] > 0 ? $account['saldo']:0 ?>
		<?php $jumlah_credit += $account['saldo']<0 ? $account['saldo']:0 ?>
	</tr>
	<?php
	if(!empty($account['children'])){
		printChildren($account['children'],$htmlHelper,$number,$jumlah_debit,$jumlah_credit,$selected_month,$selected_year);
	}?>
<?php endforeach;}?>
<?php //pr($accounts);?>

<div class="grid_12 alpha omega">
    <?php echo $this->element("nav_accounting_report")?>
</div>
	<div class="grid_12 alpha omega">
		<h2><?php echo __('Neraca Saldo')?></h2>
		<!-- START FILTER -->
		<?php echo $form->create('FilterNeracaSaldo',array('url' => '/accounting_reportings/neraca_saldo')); ?>

		<table cellpadding="0" cellspacing="0">
			<tr>
				<th>Per</th>
				<td><?php echo $form->month('FilterNeracaSaldo',$selected_month)?></td>
				<td><?php echo $form->year('FilterNeracaSaldo',2006,null,$selected_year)?></td>

				<th>
					<?php echo $form->end("Show")?>
				</th>
			</tr>
		</table>
		<!-- END FILTER -->


		<table cellpadding="0" cellspacing="0" class="list" border=1>
			<thead>
				<tr>
					<th>No.</th>
					<th>Account</th>
					<th>Debit</th>
					<th>Credit</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($accounts as $account):?>
				<?php if(empty($account['children'])):?>
				<tr>
					<td><?php echo $account['nomor'] ?></td>
					<td><?php echo !empty($account['id'])? $this->Html->link($account['name'], '/accounting_reportings/buku_besar/'.$account['id'].'/'.$selected_month.'/'.date('Y')) : $account['name']?><?php //echo $account['name'] ?></td>
					<td class="money"><?php echo $account['saldo'] > 0 ? $number->currency($account['saldo']):$number->currency(0) ?></td>
					<td class="money"><?php echo $account['saldo'] < 0 ? $number->currency($account['saldo']):$number->currency(0) ?></td>
					<?php $jumlah_debit += $account['saldo'] > 0 ? $account['saldo']:0 ?>
					<?php $jumlah_credit += $account['saldo']<0 ? $account['saldo']:0 ?>
				</tr>
				<?php else:?>
				<tr>
					<th colspan=4><?php echo $account['nomor'] ?> : <?php echo !empty($account['id'])? $this->Html->link($account['name'], '/accounting_reportings/buku_besar/'.$account['id'].'/'.$selected_month.'/'.$selected_year) : $account['name'] ?></th>
				</tr>
				<?php printChildren($account['children'],$this->Html,$number,$jumlah_debit,$jumlah_credit,$selected_month,$selected_year)?>
				<tr>
					<th colspan=4></th>
				</tr>
				<?php endif;?>

				<?php endforeach?>
			</tbody>
			<tfoot>
				<tr>
					<th colspan="2">Saldo</th>
					<th class="money"><?php echo $number->currency($jumlah_debit) ?></th>
					<th class="money"><?php echo $number->currency($jumlah_credit) ?></th>
				</tr>
			</tfoot>
		</table>
	</div>
